package com.doumi.biz.qadev.api.testcase.commonfilter;

import com.doumi.biz.qadev.api.methods.OperationRedis;
import com.doumi.biz.qadev.api.testcase.RequestTemplate;
import io.restassured.path.json.JsonPath;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * Created by YDZ on 2019/11/20.
 */
public class CommonFilterActions {
    private RequestTemplate requestTemplate = new RequestTemplate();
    private OperationRedis operationRedis = new OperationRedis();
    private static List<String> cuIds = new ArrayList<>();
    StringBuffer sb = new StringBuffer("[");

    //redis初始化数据
    @BeforeClass(description = "初始化redis数据,所有的用户信息都是正常的用户信息，并打印插入的用户信息")
    public  void operationData(){
        String filenanme = "src/source/redisfile/filter-actions.csv";
        String mothodname = "white";
        cuIds = operationRedis.readCsvoperationRedis(filenanme,mothodname,1);
        for (int i = 0; i <cuIds.size()-1;  i++) {
            sb.append(cuIds.get(i).trim()).append(",");
        }
        sb.append(cuIds.get(cuIds.size()-1).trim());
        sb.append("]");
    }
    @Test(description = "添加actions参数，当参数中传入需要过滤的actionid，则过滤掉用户中的不符合条件的id，本用例以白名单为例，当actionid为1时过滤该用户，结果中返回值为26000008，返回的msg为success")
    public void checkEligibilityActionsId(){

        HashMap hashMap = new HashMap();
        hashMap.put("cuIds",sb.toString());
        hashMap.put("businessType","1");
        hashMap.put("actions","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String msg = jsonPath.getString("msg");
        //校验每个参数
        List<String> diffUserid = new ArrayList<String>(cuIds);
        for (String userid: cuIds ) {
            for (Object aResurlt : resurlt) {
                if (Objects.equals(String.valueOf(aResurlt),String.valueOf(userid))) {
                    diffUserid.remove(userid);
                    break;
                }
            }
        }
        System.out.println(diffUserid);
        Reporter.log("有被过滤掉的数据，具体的userid:"+diffUserid);
//        Assert.assertEquals(diffUserid.size(),1);
//        Assert.assertEquals(resurlt.size(),cuIds.size());
        Assert.assertEquals(String.valueOf(resurlt.get(0)),String.valueOf(26000008));
        Assert.assertEquals(msg,"success");
    }
    @Test(description = "请求参数cuIds为空的情况下，接口返回结果code为100，msg为param cuIds must be not null，data为空")
    public void checkUneligibilityActionsId(){
        HashMap hashMap = new HashMap();
        hashMap.put("cuIds",sb.toString());
        hashMap.put("businessType","2");
        hashMap.put("actions","2");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),2);
        Assert.assertEquals(code,"200");
    }


    //删除数据
    @AfterClass(description = "删除出入掉的数据，避免影响其他测试")
    public void removeData(){
        operationRedis.removeRedis(cuIds);
    }
}
